/*   1:    */ package nl.siegmann.epublib.viewer;
/*   2:    */ 
/*   3:    */ import java.io.Reader;
/*   4:    */ import java.io.StringReader;
/*   5:    */ import java.util.HashMap;
/*   6:    */ import java.util.Map;
/*   7:    */ import java.util.concurrent.locks.Lock;
/*   8:    */ import java.util.concurrent.locks.ReentrantReadWriteLock;
/*   9:    */ import javax.swing.text.EditorKit;
/*  10:    */ import javax.swing.text.html.HTMLDocument;
/*  11:    */ import javax.swing.text.html.HTMLEditorKit;
/*  12:    */ import javax.swing.text.html.HTMLEditorKit.Parser;
/*  13:    */ import nl.siegmann.epublib.browsersupport.NavigationEvent;
/*  14:    */ import nl.siegmann.epublib.browsersupport.NavigationEventListener;
/*  15:    */ import nl.siegmann.epublib.browsersupport.Navigator;
/*  16:    */ import nl.siegmann.epublib.domain.Book;
/*  17:    */ import nl.siegmann.epublib.domain.Resource;
/*  18:    */ import nl.siegmann.epublib.domain.Resources;
/*  19:    */ import nl.siegmann.epublib.service.MediatypeService;
/*  20:    */ import org.apache.commons.io.IOUtils;
/*  21:    */ import org.slf4j.Logger;
/*  22:    */ import org.slf4j.LoggerFactory;
/*  23:    */ 
/*  24:    */ public class HTMLDocumentFactory
/*  25:    */   implements NavigationEventListener
/*  26:    */ {
/*  27: 37 */   private static final Logger log = LoggerFactory.getLogger(HTMLDocumentFactory.class);
/*  28:    */   public static final int DOCUMENT_CACHE_INDEXER_WAIT_TIME = 500;
/*  29:    */   private ImageLoaderCache imageLoaderCache;
/*  30: 45 */   private ReentrantReadWriteLock cacheLock = new ReentrantReadWriteLock();
/*  31: 46 */   private Lock cacheReadLock = this.cacheLock.readLock();
/*  32: 47 */   private Lock cacheWriteLock = this.cacheLock.writeLock();
/*  33: 48 */   private Map<String, HTMLDocument> documentCache = new HashMap();
/*  34:    */   private MyHtmlEditorKit editorKit;
/*  35:    */   
/*  36:    */   public HTMLDocumentFactory(Navigator navigator, EditorKit editorKit)
/*  37:    */   {
/*  38: 52 */     this.editorKit = new MyHtmlEditorKit((HTMLEditorKit)editorKit);
/*  39: 53 */     this.imageLoaderCache = new ImageLoaderCache(navigator);
/*  40: 54 */     init(navigator.getBook());
/*  41: 55 */     navigator.addNavigationEventListener(this);
/*  42:    */   }
/*  43:    */   
/*  44:    */   public void init(Book book)
/*  45:    */   {
/*  46: 59 */     if (book == null) {
/*  47: 60 */       return;
/*  48:    */     }
/*  49: 62 */     this.imageLoaderCache.initBook(book);
/*  50: 63 */     initDocumentCache(book);
/*  51:    */   }
/*  52:    */   
/*  53:    */   private void putDocument(Resource resource, HTMLDocument document)
/*  54:    */   {
/*  55: 67 */     if (document == null) {
/*  56: 68 */       return;
/*  57:    */     }
/*  58: 70 */     this.cacheWriteLock.lock();
/*  59:    */     try
/*  60:    */     {
/*  61: 72 */       this.documentCache.put(resource.getHref(), document);
/*  62:    */     }
/*  63:    */     finally
/*  64:    */     {
/*  65: 74 */       this.cacheWriteLock.unlock();
/*  66:    */     }
/*  67:    */   }
/*  68:    */   
/*  69:    */   public HTMLDocument getDocument(Resource resource)
/*  70:    */   {
/*  71: 90 */     HTMLDocument document = null;
/*  72:    */     
/*  73:    */ 
/*  74: 93 */     this.cacheReadLock.lock();
/*  75:    */     try
/*  76:    */     {
/*  77: 95 */       document = (HTMLDocument)this.documentCache.get(resource.getHref());
/*  78:    */     }
/*  79:    */     finally
/*  80:    */     {
/*  81: 97 */       this.cacheReadLock.unlock();
/*  82:    */     }
/*  83:101 */     if (document == null)
/*  84:    */     {
/*  85:102 */       document = createDocument(resource);
/*  86:103 */       putDocument(resource, document);
/*  87:    */     }
/*  88:107 */     if (document != null) {
/*  89:108 */       this.imageLoaderCache.initImageLoader(document);
/*  90:    */     }
/*  91:111 */     return document;
/*  92:    */   }
/*  93:    */   
/*  94:    */   private String stripHtml(String input)
/*  95:    */   {
/*  96:115 */     String result = removeControlTags(input);
/*  97:    */     
/*  98:117 */     return result;
/*  99:    */   }
/* 100:    */   
/* 101:    */   private static String removeControlTags(String input)
/* 102:    */   {
/* 103:128 */     StringBuilder result = new StringBuilder();
/* 104:129 */     boolean inControlTag = false;
/* 105:130 */     for (int i = 0; i < input.length(); i++)
/* 106:    */     {
/* 107:131 */       char c = input.charAt(i);
/* 108:132 */       if (inControlTag)
/* 109:    */       {
/* 110:133 */         if (c == '>') {
/* 111:134 */           inControlTag = false;
/* 112:    */         }
/* 113:    */       }
/* 114:136 */       else if ((c == '<') && (i < input.length() - 1) && ((input.charAt(i + 1) == '!') || (input.charAt(i + 1) == '?'))) {
/* 115:139 */         inControlTag = true;
/* 116:    */       } else {
/* 117:141 */         result.append(c);
/* 118:    */       }
/* 119:    */     }
/* 120:144 */     return result.toString();
/* 121:    */   }
/* 122:    */   
/* 123:    */   private HTMLDocument createDocument(Resource resource)
/* 124:    */   {
/* 125:156 */     HTMLDocument result = null;
/* 126:157 */     if (resource.getMediaType() != MediatypeService.XHTML) {
/* 127:158 */       return result;
/* 128:    */     }
/* 129:    */     try
/* 130:    */     {
/* 131:161 */       HTMLDocument document = (HTMLDocument)this.editorKit.createDefaultDocument();
/* 132:162 */       MyParserCallback parserCallback = new MyParserCallback(document.getReader(0));
/* 133:163 */       HTMLEditorKit.Parser parser = this.editorKit.getParser();
/* 134:164 */       String pageContent = IOUtils.toString(resource.getReader());
/* 135:165 */       pageContent = stripHtml(pageContent);
/* 136:166 */       document.remove(0, document.getLength());
/* 137:167 */       Reader contentReader = new StringReader(pageContent);
/* 138:168 */       parser.parse(contentReader, parserCallback, true);
/* 139:169 */       parserCallback.flush();
/* 140:170 */       result = document;
/* 141:    */     }
/* 142:    */     catch (Exception e)
/* 143:    */     {
/* 144:172 */       log.error(e.getMessage());
/* 145:    */     }
/* 146:174 */     return result;
/* 147:    */   }
/* 148:    */   
/* 149:    */   private void initDocumentCache(Book book)
/* 150:    */   {
/* 151:178 */     if (book == null) {
/* 152:179 */       return;
/* 153:    */     }
/* 154:181 */     this.documentCache.clear();
/* 155:182 */     Thread documentIndexerThread = new Thread(new DocumentIndexer(book), "DocumentIndexer");
/* 156:183 */     documentIndexerThread.setPriority(1);
/* 157:184 */     documentIndexerThread.start();
/* 158:    */   }
/* 159:    */   
/* 160:    */   private class DocumentIndexer
/* 161:    */     implements Runnable
/* 162:    */   {
/* 163:    */     private Book book;
/* 164:    */     
/* 165:    */     public DocumentIndexer(Book book)
/* 166:    */     {
/* 167:194 */       this.book = book;
/* 168:    */     }
/* 169:    */     
/* 170:    */     public void run()
/* 171:    */     {
/* 172:    */       try
/* 173:    */       {
/* 174:199 */         Thread.sleep(500L);
/* 175:    */       }
/* 176:    */       catch (InterruptedException e)
/* 177:    */       {
/* 178:201 */         HTMLDocumentFactory.log.error(e.getMessage());
/* 179:    */       }
/* 180:203 */       addAllDocumentsToCache(this.book);
/* 181:    */     }
/* 182:    */     
/* 183:    */     private void addAllDocumentsToCache(Book book)
/* 184:    */     {
/* 185:207 */       for (Resource resource : book.getResources().getAll()) {
/* 186:208 */         HTMLDocumentFactory.this.getDocument(resource);
/* 187:    */       }
/* 188:    */     }
/* 189:    */   }
/* 190:    */   
/* 191:    */   public void navigationPerformed(NavigationEvent navigationEvent)
/* 192:    */   {
/* 193:216 */     if ((navigationEvent.isBookChanged()) || (navigationEvent.isResourceChanged())) {
/* 194:217 */       this.imageLoaderCache.clear();
/* 195:    */     }
/* 196:    */   }
/* 197:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.viewer.HTMLDocumentFactory
 * JD-Core Version:    0.7.0.1
 */